/// @tags: Replacement
#include <algorithm>
#include <cstdio>
#include <iostream>

using namespace std;

namespace BlueQuantum {

typedef long long ll;

int const N = 1e6 + 5, INF = 0x3f3f3f3f;

int n, gloMin = INF;
int a[N], b[N], w[N], A[N];
bool vis[N];
ll ans;

inline void dfs(int x) {
  int len = 0, min = INF;
  ll sum = 0;
  while (!vis[x]) {
    vis[x] = true;
    sum += w[a[x]];
    min = std::min(min, w[a[x]]);
    x = A[b[x]];
    len++;
  }
  ans += std::min(sum + min * (len - 2), sum + min + gloMin * (len + 1));
}

inline int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> w[i], gloMin = std::min(gloMin, w[i]);
  for (int i = 1; i <= n; ++i) cin >> a[i], A[a[i]] = i;
  for (int i = 1; i <= n; ++i) cin >> b[i];
  for (int i = 1; i <= n; ++i)
    if (!vis[i]) dfs(i);
  cout << ans;
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("P3482 [POI2009] SLO-Elephants.in", "r", stdin);
  freopen("P3482 [POI2009] SLO-Elephants.out", "w", stdout);
#endif
#endif

  ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
